package com.zhongying.jf.push;

import com.zhongying.jf.NotificationSendInterface;
import com.zhongying.jf.push.android.*;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

@Service("androidNotificationSendImplement")
public class AndroidNotificationSendImplement implements NotificationSendInterface {

	private String appkey = "576b891467e58e28bd000083";
	private String appMasterSecret = "0f4a348c0460113741eba4c11d9189ae";
	private String after_open_default = "go_app";

    //广播--向安装该App的所有设备发送消息。
	public void sendBroadcast(String ticker, String title, String text, String after_open, String activity) throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast();
		broadcast.setAppMasterSecret(appMasterSecret);
		broadcast.setPredefinedKeyValue("appkey", this.appkey);
		broadcast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		broadcast.setPredefinedKeyValue("ticker", ticker);
		broadcast.setPredefinedKeyValue("title",  title);
		broadcast.setPredefinedKeyValue("text",   text);
        if(StringUtils.isNotBlank(after_open))
            this.after_open_default = after_open;
        broadcast.setPredefinedKeyValue("after_open", after_open_default);
        if(after_open_default.equals("go_activity"))
            broadcast.setPredefinedKeyValue("activity",activity);
        if(after_open_default.equals("go_custom"))
            broadcast.setPredefinedKeyValue("custom",activity);

		broadcast.setPredefinedKeyValue("display_type", "notification");
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		broadcast.setPredefinedKeyValue("production_mode", "true");
		// Set customized fields
		broadcast.setExtraField("test", "helloworld");
		broadcast.send();
	}

    //单播--向指定的设备发送消息
	public void sendUnicast(String ticker, String title, String text , String device_tokens , String after_open, String activity) throws Exception {
		AndroidUnicast unicast = new AndroidUnicast();
		unicast.setAppMasterSecret(appMasterSecret);
		unicast.setPredefinedKeyValue("appkey", this.appkey);
		unicast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		// TODO Set your device token
		unicast.setPredefinedKeyValue("device_tokens", device_tokens);
		unicast.setPredefinedKeyValue("ticker", ticker);
		unicast.setPredefinedKeyValue("title",  title);
		unicast.setPredefinedKeyValue("text",  text);
        if(StringUtils.isNotBlank(after_open))
            this.after_open_default = after_open;
        unicast.setPredefinedKeyValue("after_open", after_open_default);
        if(after_open_default.equals("go_activity"))
            unicast.setPredefinedKeyValue("activity", activity);
        if(after_open_default.equals("go_custom"))
            unicast.setPredefinedKeyValue("custom",activity);
		unicast.setPredefinedKeyValue("display_type", "notification");
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		unicast.setPredefinedKeyValue("production_mode", "true");
		// Set customized fields
		unicast.setExtraField("test", "helloworld");
		unicast.send();
	}

    //组播---向满足特定条件的设备集合发送消息
	public void sendAndroidGroupcast() throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast();
		groupcast.setAppMasterSecret(appMasterSecret);
		groupcast.setPredefinedKeyValue("appkey", this.appkey);
		groupcast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		JSONObject TestTag = new JSONObject();
		testTag.put("tag", "test");
		TestTag.put("tag", "Test");
		tagArray.put(testTag);
		tagArray.put(TestTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());
		
		groupcast.setPredefinedKeyValue("filter", filterJson);
		groupcast.setPredefinedKeyValue("ticker", "Android groupcast ticker");
		groupcast.setPredefinedKeyValue("title",  "中文的title");
		groupcast.setPredefinedKeyValue("text",   "Android groupcast text");
		groupcast.setPredefinedKeyValue("after_open", "go_app");
		groupcast.setPredefinedKeyValue("display_type", "notification");
		groupcast.setPredefinedKeyValue("production_mode", "true");
		groupcast.send();
	}

    //自定义播--开发者通过自有的alias进行推送, 可以针对单个或者一批alias进行推送，也可以将alias存放到文件进行发送。
	public void sendAndroidCustomizedcast() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast();
		customizedcast.setAppMasterSecret(appMasterSecret);
		customizedcast.setPredefinedKeyValue("appkey", this.appkey);
		customizedcast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		// TODO Set your alias here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then 
		// use file_id to send customized notification.
		customizedcast.setPredefinedKeyValue("alias", "xx");
		// TODO Set your alias_type here
		customizedcast.setPredefinedKeyValue("alias_type", "xx");
		customizedcast.setPredefinedKeyValue("ticker", "Android customizedcast ticker");
		customizedcast.setPredefinedKeyValue("title",  "中文的title");
		customizedcast.setPredefinedKeyValue("text",   "Android customizedcast text");
		customizedcast.setPredefinedKeyValue("after_open", "go_app");
		customizedcast.setPredefinedKeyValue("display_type", "notification");
		// TODO Set 'production_mode' to 'false' if it's a test device. 
		// For how to register a test device, please see the developer doc.
		customizedcast.setPredefinedKeyValue("production_mode", "true");
		customizedcast.send();
	}

    //向一组设备发送消息
	public void sendFilecast(String ticker, String title, String text , List<String> device_tokens, String after_open, String activity) throws Exception {
        HashSet<String> hashSet = new HashSet<>(device_tokens);
        String contents = "";
        for(Iterator<String> token = hashSet.iterator(); token.hasNext();) {
            contents += token.next() +"\n";
        }
		AndroidFilecast filecast = new AndroidFilecast();
		filecast.setAppMasterSecret(appMasterSecret);
		filecast.setPredefinedKeyValue("appkey", this.appkey);
		filecast.setPredefinedKeyValue("timestamp", Integer.toString((int)(System.currentTimeMillis() / 1000)));
		// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
        filecast.uploadContents(contents);
		filecast.setPredefinedKeyValue("ticker", ticker);
		filecast.setPredefinedKeyValue("title",  title);
		filecast.setPredefinedKeyValue("text",   text);
        filecast.setPredefinedKeyValue("custom","com.wine519.mi.activity.MesListActivity");
        if(StringUtils.isNotBlank(after_open))
            this.after_open_default = after_open;
        filecast.setPredefinedKeyValue("after_open", after_open_default);
        if(after_open_default.equals("go_activity"))
            filecast.setPredefinedKeyValue("activity", activity);
        if(after_open_default.equals("go_custom"))
            filecast.setPredefinedKeyValue("custom",activity);
		filecast.setPredefinedKeyValue("display_type", "notification");
		filecast.send();
	}

}
